import React, {Component} from 'react';
import {Route, Redirect} from 'react-router-dom';
import Login from './Login';
import {Menu, Icon} from 'antd';
import Cmd from './Cmd';
import DefaultParams from './DefaultParams';
import {getLogin, validateToken, cleanInfo} from '../../utils/login';
import './entry.scss';
const SubMenu = Menu.SubMenu;

export default class Admin extends Component {
  state = {
    selectKeys: [],
  };
  menuClick = ({key}) => {
    this.props.history.push (key);
  };
  validateLoin = async function () {
    const token = window.localStorage.getItem ('fxams-token');
    const opd = window.localStorage.getItem ('opd');
    let needLogin = false;
    let vaildToken = false;
    if (token) {
      try {
        const status = await validateToken ();
        if (status == 1) {
          vaildToken = true;
        }
      } catch (err) {
        console.log (err);
        needLogin = true;
        vaildToken = false;
        cleanInfo ();
      }
    }
    if (!token || !opd || needLogin || !vaildToken) {
      await getLogin ();
    } else {
      console.log ('跳转');
    }
  };
  componentDidMount () {
    this.validateLoin();
  }
  render () {
    let selectKeys = [this.props.history.location.pathname];
    return (
      <div className="admin-index">
        <p className="admin-header">app拉起配置管理</p>
        <div className="admin-inner">
          <div className="left-side">
            <Menu
              style={{width: 256}}
              mode="inline"
              onClick={this.menuClick}
              defaultOpenKeys={['sub1']}
              selectedKeys={selectKeys}
            >
              <SubMenu
                key="sub1"
                title={<span><Icon type="android" /><span>拉起设置</span></span>}
              >
                <Menu.Item key="/admin/">CMD添加</Menu.Item>
                <Menu.Item key="/admin/paramsSet">参数管理</Menu.Item>
              </SubMenu>
            </Menu>
          </div>
          <div className="right-side">
            <Route exact path="/admin/" component={Cmd} />
            <Route exact path="/admin/paramsSet" component={DefaultParams} />
          </div>
        </div>
      </div>
    );
  }
}
